package com.businesscard.system.service.impl;

import com.businesscard.system.entity.Admin;
import com.businesscard.system.entity.User;
import com.businesscard.system.mapper.AdminMapper;
import com.businesscard.system.mapper.UserMapper;
import com.businesscard.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 管理员服务实现类
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public Admin login(String username, String password) {
        Admin admin = adminMapper.selectByUsername(username);
        
        if (admin == null) {
            return null;
        }
        
        // 验证密码
        if (passwordEncoder.matches(password, admin.getPassword())) {
            // 登录成功，更新登录时间
            adminMapper.updateLoginTime(admin.getId());
            return admin;
        } else {
            return null;
        }
    }

    @Override
    public Admin getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public Admin getAdminByUsername(String username) {
        return adminMapper.selectByUsername(username);
    }

    @Override
    public Admin getAdminByEmail(String email) {
        return adminMapper.selectByEmail(email);
    }

    @Override
    public List<Admin> getAllAdmins() {
        return adminMapper.selectAll();
    }

    @Override
    @Transactional
    public boolean addAdmin(Admin admin) {
        // 检查用户名是否已存在
        if (adminMapper.selectByUsername(admin.getUsername()) != null) {
            return false;
        }
        
        // 检查邮箱是否已存在
        if (adminMapper.selectByEmail(admin.getEmail()) != null) {
            return false;
        }
        
        // 加密密码
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        
        // 插入管理员
        return adminMapper.insert(admin) > 0;
    }

    @Override
    @Transactional
    public boolean updateAdmin(Admin admin) {
        Admin existingAdmin = adminMapper.selectById(admin.getId());
        if (existingAdmin == null) {
            return false;
        }
        
        return adminMapper.update(admin) > 0;
    }

    @Override
    @Transactional
    public boolean deleteAdmin(Integer id) {
        return adminMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean changePassword(Integer id, String oldPassword, String newPassword) {
        Admin admin = adminMapper.selectById(id);
        if (admin == null) {
            return false;
        }
        
        // 验证旧密码
        if (!passwordEncoder.matches(oldPassword, admin.getPassword())) {
            return false;
        }
        
        // 加密新密码
        String encodedPassword = passwordEncoder.encode(newPassword);
        
        return adminMapper.updatePassword(id, encodedPassword) > 0;
    }

    @Override
    @Transactional
    public boolean promoteUserToAdmin(Integer userId) {
        // 查询用户
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }
        
        // 检查是否已经是管理员
        if ("ADMIN".equals(user.getRole())) {
            return true;
        }
        
        // 更新用户角色为管理员
        user.setRole("ADMIN");
        
        // 创建并插入管理员记录
        Admin admin = new Admin();
        admin.setUsername(user.getUsername());
        admin.setPassword(user.getPassword()); // 使用相同的密码
        admin.setEmail(user.getEmail());
        
        // 先插入管理员记录，再更新用户
        boolean adminInserted = adminMapper.insert(admin) > 0;
        boolean userUpdated = userMapper.update(user) > 0;
        
        return adminInserted && userUpdated;
    }
} 